package com.anywhere.freightman.car.controller;

import com.anywhere.freightman.car.controller.request.DriverRequest;
import com.anywhere.freightman.car.controller.resp.DriverResp;
import com.anywhere.freightman.car.model.DriverModel;
import com.anywhere.freightman.car.service.IDriverService;
import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.entity.user.DriverQueryEntity;
import com.anywhere.freightman.common.enumeration.DriverStatus;
import com.anywhere.freightman.common.enumeration.DriverType;
import com.anywhere.freightman.common.enumeration.RoleType;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.common.request.PageRequest;
import com.anywhere.freightman.common.utils.BPwdEncoderUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("driver")
public class DriverController {

    @Autowired
    private IDriverService driverService;

    @Permission(code = "DRIVER")
    @GetMapping("page")
    @ApiOperation("分页")
    public PageInfo<DriverModel> getPageList(HttpServletRequest httpRequest,
                                             DriverQueryEntity qm) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            qm.setCompanyId(account.getCompanyId());
        }

        PageRequest<DriverQueryEntity> pageRequest = new PageRequest<>();
        BeanUtils.copyProperties(qm, pageRequest);
        pageRequest.setEntity(qm);
        return driverService.getPageList(pageRequest);
    }

    @PostMapping
    @Permission(code = "DRIVER")
    public DriverModel insertData(HttpServletRequest httpRequest,
                                  @RequestBody DriverRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);

        DriverModel model = new DriverModel();
        BeanUtils.copyProperties(request, model);
        if (!account.getType().equals(RoleType.ADMIN)) {
            model.setCompanyId(account.getCompanyId());
        }
        model.setStatus(DriverStatus.REST);
        model.setInsertTime(new Date());
        if (request.getType() == null) {
            model.setType(DriverType.OWN);
        }
        model.setPassword(BPwdEncoderUtils.BCryptPassword("000000"));
        driverService.insert(model);
        return model;
    }

    @PutMapping
    @Permission(code = "DRIVER")
    public DriverModel updateData(HttpServletRequest httpRequest,
                                  @RequestBody DriverRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        DriverModel model = driverService.getById(request.getId());
        if (model == null || (!account.getType().equals(RoleType.ADMIN) && !account.getCompanyId().equals(model.getCompanyId()))) {
            throw new CustomException("司机信息不存在");
        }
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(account.getCompanyId());
        driverService.update(model);
        return model;
    }

    @DeleteMapping
    @ApiOperation("删除")
    @Permission(code = "DRIVER")
    public ReturnResp delete(HttpServletRequest httpRequest,
                             @RequestParam("id") long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        DriverModel model = driverService.getById(id);
        if (model != null && account.getCompanyId().equals(model.getCompanyId())) {
            driverService.delete(model);
            return ReturnResp.buildOk();
        } else {
            throw new CustomException("司机信息不存在");
        }
    }

    @GetMapping("{id}")
    @ApiOperation("详情")
    @Permission(code = "DRIVER")
    public ReturnResp getDetails(HttpServletRequest httpRequest,
                                 @PathVariable("id") long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        DriverModel model = driverService.getById(id);
        DriverResp resp = new DriverResp();
        if (model == null || (!account.getType().equals(RoleType.ADMIN) && !account.getCompanyId().equals(model.getCompanyId()))) {
            throw new CustomException("信息不存在");
        }
        BeanUtils.copyProperties(model, resp);
        return ReturnResp.buildOk(resp);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @Permission(code = "DRIVER")
    public ReturnResp remove(@PathVariable("id") long id) {
        driverService.remove(id);
        return ReturnResp.buildOk();
    }

}
